<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8">
        <div class="app-container">
          <el-row>
            <el-col :span="18">
            </el-col>
                    <el-col :span="16" style="float:right;">
            <el-button-group style="float: right">
              <el-button
                type="primary"
                icon="el-icon-plus"
                class="el-button--mini"
                @click="handleCreate"
                >添加</el-button
              >
              <el-button type="primary" size="mini" @click="handleUpdate()">
                编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="ui_delete(null,null)"
              >
                删除
              </el-button>
            </el-button-group>
            </el-col>
          </el-row>
          <el-row type="flex" justify="start">
            <el-table
              v-loading="listLoading"
              :data="list"
              element-loading-text="Loading"
              border
              fit
              highlight-current-row
              @row-click="rowClick"
              @selection-change="handleSelectionChange">
            >
            <el-table-column
            type="selection"
             width="55"
            ></el-table-column>
                <el-table-column align="center" label="scode" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.scode }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="sparentcode" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.sparentcode }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="scategoryname" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.scategoryname }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="smaterialtype" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.smaterialtype }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="sremark" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.sremark }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="bmaincategory" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.bmaincategory }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="screator" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.screator }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="dcreatedate" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.dcreatedate }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="busable" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.busable }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="spurchaserlist" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.spurchaserlist }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="sworksectionidlist" width="95" style="display:none;">
                <template slot-scope="scope">
                  {{ scope.row.sworksectionidlist }}
                </template>
              </el-table-column>
            </el-table>
              </el-row>
        </div>
      </el-col>
      <el-col :span="16">
        <div class="app-container">
          <el-row>
            <el-button-group style="float: right">
              <el-button
                type="primary"
                icon="el-icon-plus"
                class="el-button--mini"
                @click="handleCreate"
                >添加</el-button
              >
              <el-button type="primary" size="mini"> 编辑 </el-button>
              <el-button size="mini" type="danger"> 删除 </el-button>
            </el-button-group>
          </el-row>
        </div>
      </el-col>
    </el-row>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="120px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="scode" prop="scode">
          <el-input v-model="temp.scode" />
        </el-form-item>
        <el-form-item label="sparentcode" prop="sparentcode">
          <el-input v-model="temp.sparentcode" />
        </el-form-item>
        <el-form-item label="scategoryname" prop="scategoryname">
          <el-input v-model="temp.scategoryname" />
        </el-form-item>
        <el-form-item label="smaterialtype" prop="smaterialtype">
          <el-input v-model="temp.smaterialtype" />
        </el-form-item>
        <el-form-item label="sremark" prop="sremark">
          <el-input v-model="temp.sremark" />
        </el-form-item>
        <el-form-item label="bmaincategory" prop="bmaincategory">
          <el-input v-model="temp.bmaincategory" />
        </el-form-item>
        <el-form-item label="screator" prop="screator">
          <el-input v-model="temp.screator" />
        </el-form-item>
        <el-form-item label="dcreatedate" prop="dcreatedate">
          <el-input v-model="temp.dcreatedate" />
        </el-form-item>
        <el-form-item label="busable" prop="busable">
          <el-input v-model="temp.busable" />
        </el-form-item>
        <el-form-item label="spurchaserlist" prop="spurchaserlist">
          <el-input v-model="temp.spurchaserlist" />
        </el-form-item>
        <el-form-item label="sworksectionidlist" prop="sworksectionidlist">
          <el-input v-model="temp.sworksectionidlist" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消 </el-button>
        <el-button
          type="primary"
          @click="
            dialogStatus === 'create'
              ? ui_create()
              : ui_update()
          "
        >
          确定
        </el-button>
      </div>
    </el-dialog>
    <router-view></router-view>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import request from "@/utils/request";
import {
  getAll,
  getOne,
  create,
  update,
  deleteBatch
} from "@/api/mesMMMaterialCategory";

export default {
  components: { Pagination },
  data() {
    return {
      filterText: "",
      data2: [],
      defaultProps: {
        children: "children",
        label: "MMMaterialCategory",
      },
      list: null,
      list2: null,
      listLoading: true,
      total: 0,
      total2: 0,
      listQuery: {
              page: 1,
              limit: 6,
              pageSizes: [6, 10, 14],
            },
      listQuery2: {
        page: 1,
        limit: 6,
        pageSizes: [6, 10, 14],
      },
      currentMMMaterialCategoryId: 0,
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      rules: {
        scode: [
          {
            required: true,
            message: "scode不能为空！",
            trigger: "change",
          },
        ],
        sparentcode: [
          {
            required: true,
            message: "sparentcode不能为空！",
            trigger: "change",
          },
        ],
        scategoryname: [
          {
            required: true,
            message: "scategoryname不能为空！",
            trigger: "change",
          },
        ],
        smaterialtype: [
          {
            required: true,
            message: "smaterialtype不能为空！",
            trigger: "change",
          },
        ],
        sremark: [
          {
            required: true,
            message: "sremark不能为空！",
            trigger: "change",
          },
        ],
        bmaincategory: [
          {
            required: true,
            message: "bmaincategory不能为空！",
            trigger: "change",
          },
        ],
        screator: [
          {
            required: true,
            message: "screator不能为空！",
            trigger: "change",
          },
        ],
        dcreatedate: [
          {
            required: true,
            message: "dcreatedate不能为空！",
            trigger: "change",
          },
        ],
        busable: [
          {
            required: true,
            message: "busable不能为空！",
            trigger: "change",
          },
        ],
        spurchaserlist: [
          {
            required: true,
            message: "spurchaserlist不能为空！",
            trigger: "change",
          },
        ],
        sworksectionidlist: [
          {
            required: true,
            message: "sworksectionidlist不能为空！",
            trigger: "change",
          },
        ],
      },
      temp: {
       id: undefined,
       scode : '',
       sparentcode : '',
       scategoryname : '',
       smaterialtype : '',
       sremark : '',
       bmaincategory : '',
       screator : '',
       dcreatedate : '',
       busable : '',
       spurchaserlist : '',
       sworksectionidlist : '',
      },
      //表格變量
      lstSelected: []
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree2.filter(val)
    },
  },
  created() {
    this.fetchData();
  },
  methods: {
    handleSelectionChange (val) {
       this.lstSelected = val
    },
    fetchData() {
      this.listLoading = true
      let params = {
        name: ''
      }
      getAll(params).then((response) => {
        this.list = response.data
        this.listLoading = false
      })
    },

    rowClick(row, column, event) {
      this.currentTherbligsTypeId = row.id
      //this.getTherbligs()
    },
    ui_create() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.temp.id = 0; //自增
          create(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: "Success",
              message: "添加成功！",
              type: "success",
              duration: 2000,
            })
          })
        }
      })
    },
    ui_update() {
     
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          update(this.temp).then(() => {
            const index = this.list.findIndex((v) => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: "Success",
              message: "修改成功！",
              type: "success",
              duration: 2000,
            })
          })
        }
      });
    },
    ui_delete(row, index) {
       if(this.lstSelected == null || this.lstSelected.length === 0)
      {
         this.$message.error('请先选择要删除的MMMaterialCategory')
         return
      }
      let ids = []
      this.lstSelected.map(function(elm){
         ids.push(elm.id)
      })
       debugger
      deleteBatch(ids).then(() => {
        ids.forEach((elm) => {
         const index = this.list.findIndex((v) => v.id === elm)
         this.list.splice(index, 1)
        })
       
        this.dialogFormVisible = false
        this.$notify({
          title: "Success",
          message: "删除成功！",
          type: "success",
          duration: 2000,
        })
      })
    },
    resetTemp() {
      this.temp = {
       id: undefined,
              scode : '',
              sparentcode : '',
              scategoryname : '',
              smaterialtype : '',
              sremark : '',
              bmaincategory : '',
              screator : '',
              dcreatedate : '',
              busable : '',
              spurchaserlist : '',
              sworksectionidlist : '',
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = "create"
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate()
      })
    },
    handleUpdate() {
      if(this.lstSelected == null || this.lstSelected.length !==1)
      {
         this.$message.error('请先选择一条MMMaterialCategory')
         return
      }
      this.temp = Object.assign({}, this.lstSelected[0]); // copy obj
      this.dialogStatus = "update"
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate()
      })
    },
  },
}
</script>
<style>
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
